#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This component display a screen with thanks.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XAEnima.Core.GameManager;
using XAEnima.gui;
using SharedContent;
#endregion

namespace XAEnima.Core.Screens.Menu
{
    public delegate void CreditsAction();
    public class Credits : Microsoft.Xna.Framework.DrawableGameComponent
    {
        // Gli eventi dello Screen corrente
        public event CreditsAction OnQuit;

        SpriteBatch spriteBatch;
        Texture2D tex,credits;
        double counter = 0;
        IInputManager InputManagerService;
        float screenscale;
        Rectangle area;
        

        public Credits(Game game)
            : base(game)
        {
            InputManagerService = (IInputManager)Game.Services.GetService(typeof(IInputManager));
            InputManagerService.EnterPressed += new PressedEventHandler(CloseCredits);
            InputManagerService.EnterReleased += new PressedEventHandler(CloseCredits);
            InputManagerService.EscapePressed += new PressedEventHandler(CloseCredits);
            InputManagerService.TabPressed += new PressedEventHandler(CloseCredits);
            
 
        }

        
        public override void Initialize()
        {
            screenscale = (float)(Game.GraphicsDevice.Viewport.Width / 1600f);
            spriteBatch = (Game.Services.GetService(typeof(SharedContext)) as SharedContext).sprite_batch;
            area = Game.GraphicsDevice.Viewport.TitleSafeArea;

            tex = Game.Content.Load<Texture2D>(@"sfondoCredits");
            credits = Game.Content.Load<Texture2D>(@"Credits");
            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {

            base.Update(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Content.Unload();
            Game.Services.RemoveService(typeof(Credits));
            base.Dispose(disposing);
        }

        public void Detach()
        {
            // Detach the event and delete the list
            InputManagerService.EnterPressed -= new PressedEventHandler(CloseCredits);
            InputManagerService.EnterReleased -= new PressedEventHandler(CloseCredits);
            InputManagerService.EscapePressed -= new PressedEventHandler(CloseCredits);
            InputManagerService.TabPressed -= new PressedEventHandler(CloseCredits);
            InputManagerService = null;
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(tex, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.Draw(credits, Vector2.Zero, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void CloseCredits() {
            if (OnQuit != null) { OnQuit(); }
        }
    }
}
